<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage PluginsFunction
 */
/**
 * Smarty {counter} function plugin
 *
 * Type: function<br>
 * Name: counter<br>
 * Purpose: print out a counter value
 *
 * @author Monte Ohrt <monte at ohrt dot com>
 * @link http://www.smarty.net/manual/en/language.function.counter.php {counter}
 * (Smarty online manual)
 * @param array $params
 * parameters
 * @param Smarty_Internal_Template $template
 * template object
 * @return string null
 */
function smarty_function_content($params, $template)
{
    // 获取制定分类的内容
    if (isset($params['id']) && ! isset($params['cid'])) {
        $rs = NewsModel::instance()->getNewsContent($params['id'], 1, null, 1);
        $template->assign($params['assign'], $rs);
    }
    if (isset($params['type']) && $params['type'] == 'image') {
        
        $rs = NewsModel::instance()->getNewsContent($params['id'], 1, null, 1);
        //对内容进行处理
        //将单引号 全部为双引号
        //   $rs['content'] = str_replace("'", "\"", $rs['content']);
        
        preg_match_all("#<img src=\"(.*).jpg\"#iU", $rs['content'], $a);
        preg_match_all("#title=\"(.*)\"#iU", $rs['content'], $b);
        
        $images = array();
        foreach ($a[1] as $k => $v) {
            if (preg_match("#width#", $v) == TRUE) {
                Benchmark::println("匹配图片失败！内容为" . $params['id']);
                continue;
            }
            $images[$k]['thumb'] = dirname($v) . "/small_" . basename($v) . ".jpg";
            
            //检测文件是否存在 为了兼容旧版本
            if (! file_exists(ROOT . $images[$k]['thumb'])) {
                $images[$k]['thumb'] = NULL;
            }
            $images[$k]['source'] = $v . ".jpg";
            $images[$k]['title'] = $b[1][$k];
            if (isset($params['num']) && $k >= $params['num'] - 1) {
                break;
            }
        }
        $template->assign($params['assign'], $images);
    }
    //这里只有跟cid有关
    if (isset($params['cid'])) {
        
        $inId = getSon($params['cid']);
        if (empty($inId)) {
            $inId = $params['cid'];
        }
        
        $rs = NewsModel::instance()->getNewsContent($params['id'], $params['num'], null, 1);
        $template->assign($params['assign'], $rs);
    }
}
function getSon($id = 1)
{
    
    $rs = CategoryModel::instance()->sort2array($id);
    foreach ($rs as $v) {
        //回收站
        if ($v['id'] == 1067) {
            continue;
        }
        $newids .= $v['Sort']['id'] . ',';
    }
    $newids = substr($newids, 0, strlen($newids) - 1);
    return $newids;

}
?>